<template>
  <div>
    <div class="header">
      <div v-if="isLogin">
        <div class="header-title">请登录</div>
        <div class="header-info">Please Login Your Account</div>
      </div>
      <div v-else>
        <div class="header-title">请注册</div>
        <div class="header-info">Please Register Your Account</div>
      </div>
    </div>

    <div class="body">
      <div class="login-form">
        
        <van-field :value="inputUserName" label="用户名" placeholder="请输入用户名" @change="onUserNameChange"/>
        <van-field type="password" label="密码" placeholder="请输入密码" :value="inputPassWd" @change="onPassWdChange"/>
        <div v-if="!isLogin">
           <van-field :value="inputContact" label="手机号" placeholder="请输入找回密码绑定手机号" @change="onContactChange"/>
        </div>
      </div>
      <van-button slot="button" round bloc color="#3d7ef9" @click="onClick">{{isLogin?'登录':'注册'}}</van-button>
      <div class="other-option">
        <div @click="onOptionClick">
          <span >{{isLogin?'注册账户':'登录账户'}}</span>
        </div>
        <span style="margin:0 30px">|</span>

        <div @click="onForgetClick"> 
          <span>忘记密码</span>
        </div>
       
      </div>
      <div class="copyright-wrapper">
        <span class="copyright">Power By em</span>
      </div>


      <van-dialog
        use-slot
        title="找回密码校验"
        :show="showFindPW"
        show-cancel-button
        transition="fade"
        @confirm="onFindPWConfirm"
        @cancel="onFindPWCancel"
      >
        <van-field :value="inputContact" label="手机号" placeholder="请输入找回密码绑定手机号" @change="onContactChange"/>
      </van-dialog>
      <van-toast id="van-toast" />


      <van-dialog
        use-slot
        title="重置密码"
        :show="showResetPW"
        show-cancel-button
        transition="fade"
        @confirm="onResetPWConfirm"
        @cancel="onResetPWCancel"
      >
        <van-field label="用户名" title-width="60px" :value="inputUserName" readonly />
        <van-field :value="inputPassWd" label="新密码" title-width="60px" placeholder="请输入新密码" @change="onPassWdChange" type="password"  />
      </van-dialog>
      <van-toast id="van-toast" />
    </div>
  </div>
</template>
 
<script>
import Toast from "vant-weapp/dist/toast/toast"
export default {
  data () {
    return {  
      isLogin:true,
      inputUserName:"",
      inputPassWd:'',
      inputContact:'',
      showFindPW:false,
      showResetPW:false
    }
  },
  methods:{
    onUserNameChange(event){
      var that=this
      that.$set(that,"inputUserName",event.mp.detail)
      console.log("当前输入的用户名=",event.mp.detail)
    },
    onPassWdChange(event){
      var that=this
      that.$set(that,"inputPassWd",event.mp.detail)
      console.log("当前输入的密码=",event.mp.detail)
    },
    onContactChange(event){
      var that=this
      that.$set(that,"inputContact",event.mp.detail)
      console.log("当前输入的手机号=",event.mp.detail)
    },
    onClick(event){
      var that=this
      Toast.loading({
        duration:0,
        forbidClick:true,
        message:that.isLogin?'登录中...':'注册中...'
      })
      setTimeout(()=>{
        if(!that.isLogin){
          wx.setStorage({
            key:"user",
            data:{
              username:that.inputUserName,
              password:that.inputPassWd,
              contect:that.inputContact
            },
            success(res){
              console.log(res);
               console.log("注册成功");
               Toast.success('注册成功')
            },
            fail(res){
              console.log(res);
              console.log("注册失败");
              Toast.fail('注册失败')
            }
          });
        }else{
           wx.getStorage({
            key:"user",
            data:{
              username:that.inputUserName,
              password:that.inputPassWd,
              contect:that.inputContact
            },
            success(res){
              console.log(res.data);
               if(that.inputUserName===res.data.username&&
               that.inputPassWd===res.data.password){
               Toast.success('登录成功')
               setTimeout(()=>{
                 wx.switchTab({
                   url: "/pages/index/main"
                 });
               },500)
               }else{
                 Toast.fail('用户名或密码错误')
               }
            },
            fail(res){
              console.log(res)
              Toast.fail('数据库暂无注册用户')
            }
          });
        }
      },1000);
    },
    onOptionClick(event){
      var that = this
      that.isLogin=!that.isLogin
      console.log(`切换注册/登录按钮被点击了！当前处于${
        that.isLogin?"登录":"注册"
        }状态`
        );
    },
    onForgetClick(event){
      var that = this
      that.showFindPW=true
    },
    onFindPWConfirm(event){
      var that = this
       wx.getStorage({
            key:"user",
            success(res){
              console.log(res.data);
               if(that.inputContact===res.data.contect){
                 that.inputUserName=res.data.username
                 that.showResetPW=true
                 console.log("找到用户：", res.data.username)
               }else{
                 Toast.fail('无该用户信息')
                 that.inputContact=""
               }
            },
            fail(res){
              console.log(res)
              Toast.fail('数据库暂无注册用户')
              that.inputContact=""
            }
          });
    },
    onRestPWConfirm(event){
      var that = this
       wx.setStorage({
            key:"user",
            data:{
              username:that.inputUserName,
              password:that.inputPassWd,
              contect:that.inputContact
            },
            success(res){
              console.log(res);
               console.log("密码重置成功");
               Toast.success('密码重置成功')
            },
            fail(res){
              console.log(res);
              console.log("密码重置失败");
              Toast.fail('密码重置失败')
            }
          });
    },
    onResetPWCancel(event){
      var that = this
      that.showResetPW=false
      that.inputUserName=""
      that.inputPassWd=""
      that.inputContact=""
    },
    onFindPWCancel(event){
      var that = this
      that.showFindPW=false
      that.inputContact=""
    }

  }


}
</script>

<style lang="scss" scoped>
.header{
  height: 100px;
  padding: 25px 0;
  background-color: #3d7ef9;
  .header-title{
    font-size:28px;
    font-weight:500;
    color: #fff;
    margin-left: 20px;
  }
  .header-info{
    font-size:28px;
    color: #fff;
    margin-left: 20px;
  }
  
}
.body{
  padding: 20px;
  margin-top: -20px;
  border-radius: 15px 15px 0 0;
  background-color: #fff;
  .login-form{
    margin-bottom:10px;
    
  }
  .other-option{
    display:flex;
    justify-content: center;
    margin-top:20px;
    color:#9f9f9f;
  }
  .copyright-wrapper{
    display: flex;
    justify-content: center;
    .copyright{
      color: #d6d6d6;
      position:fixed;
      bottom:50px;
    }
  }
}

</style>
